package com.zhiyiang.service.order;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiyiang.Vo.OrderVO;
import com.zhiyiang.mapper.HotelMapper;
import com.zhiyiang.mapper.OrdersMapper;
import com.zhiyiang.mapper.RoomMapper;
import com.zhiyiang.mapper.UserMapper;
import com.zhiyiang.pojo.Hotel;
import com.zhiyiang.pojo.Orders;
import com.zhiyiang.pojo.Room;
import com.zhiyiang.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderServiceImpl implements OrderService{

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private HotelMapper hotelMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public List<Orders> findAll() {
        List<Orders> orders = ordersMapper.selectList(null);
        return orders;
    }

    @Override
    public Page<OrderVO> getAllOrders(Integer current, Integer size) {
        Page<Orders> page = new Page<>(current, size);
        Page<Orders> orderPage = ordersMapper.selectPage(page, null);
        List<OrderVO> records= new ArrayList<>();
//        Map result = new HashMap();
        for (Orders order :
                orderPage.getRecords()) {
//            int i = 0;
            User user = userMapper.selectById(order.getOrderuser());
            Hotel hotel = hotelMapper.selectById(order.getOrderhotel());
            QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roomcode",order.getRoomcode()).eq("hotelid", hotel.getId());
            Room room = roomMapper.selectOne(queryWrapper);
            OrderVO orderVO = new OrderVO();
            orderVO.setId(order.getId());
            orderVO.setCreatedate(order.getCreatedate());
            orderVO.setRoomstate(room.getRoomstate());
            orderVO.setOrderuser(user.getUsername());
            orderVO.setRoomtype(room.getRoomtype());
            orderVO.setOrderhotel(hotel.getHotelname());
            orderVO.setOrdercompany(order.getOrdercompany());
            orderVO.setOrdernumber(order.getOrdernumber());
            orderVO.setIndate(order.getIndate());
            orderVO.setOutdate(order.getOutdate());
            orderVO.setOrderprice(order.getOrderprice());
            orderVO.setRoomcode(order.getRoomcode());
            records.add(orderVO);
//            i=i+1;
        }
        Page<OrderVO> orderVOPage = new Page<OrderVO>(orderPage.getCurrent(), orderPage.getSize(), orderPage.getTotal());
        orderVOPage.setRecords(records);
        orderVOPage.setOrders(orderPage.getOrders());
        orderVOPage.setCountId(orderPage.getCountId());
        orderVOPage.setPages(orderPage.getPages());
        orderVOPage.setMaxLimit(orderPage.getMaxLimit());
        System.out.println(orderVOPage);
        return orderVOPage;
    }

    @Override
    public Page<Orders> getOrders(String query, Integer current, Integer size) {
        Page<Orders> page = new Page<>(current, size);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasLength(query),"hotelid", query);
        Page<Orders> orderPage = ordersMapper.selectPage(page, queryWrapper);
        return orderPage;
    }

    @Override
    public Boolean addOrders(OrderVO orders) {
        Boolean flag = false;
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",orders.getOrderuser());
        User user = userMapper.selectOne(queryWrapper);
        QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("hotelname",orders.getOrderhotel());
        System.out.println(orders.getOrderhotel());
        Hotel hotel = hotelMapper.selectOne(queryWrapper1);
        QueryWrapper<Room> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("roomcode",orders.getRoomcode());
        Room room = roomMapper.selectOne(queryWrapper2);
        room.setRoomstate(2);
        roomMapper.updateById(room);
        Orders orders1 = new Orders();
        orders1.setOrdercompany(orders.getOrdercompany());
        orders1.setOrderhotel(hotel.getId());
        orders1.setOrderuser(user.getId());
        orders1.setOrdernumber(orders.getOrdernumber());
        orders1.setOrderprice(orders.getOrderprice());
        orders1.setCreatedate(new Date());
        orders1.setIndate(orders.getIndate());
        orders1.setOutdate(orders.getOutdate());
        orders1.setRoomcode(orders.getRoomcode());
        int insert = ordersMapper.insert(orders1);
        if (insert>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Boolean updateOrders(String id, Orders orders) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(id),"id", id);
        int update = ordersMapper.update(orders,queryWrapper);
        boolean flag = false;
        if (update>0){
            flag=true;
        }
        return flag;
    }

    @Override
    public Boolean deleteOrder(String id) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(id), "id", id);
        int delete = ordersMapper.delete(queryWrapper);
        Boolean flag = false;
        if (delete>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Orders getOrders(Integer id) {
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(String.valueOf(id)),"id", id);
        Orders order = ordersMapper.selectOne(queryWrapper);
        return order;
    }

    @Override
    public Page<OrderVO> findUserByPage(String usercode,String query,Integer pagenum,Integer pagesize ) {
        Page<Orders> ordersPage = new Page<>(pagenum,pagesize);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usercode", usercode);
        User user = userMapper.selectOne(queryWrapper);
        QueryWrapper<Orders> queryWrapper1 = new QueryWrapper<>();
        if (user.getRole()==2){
            QueryWrapper<User> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("username", query);
            User user1 = userMapper.selectOne(queryWrapper);
            QueryWrapper<Hotel> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("userid", user.getId());
            Hotel hotel = hotelMapper.selectOne(queryWrapper2);
            queryWrapper1.eq("orderhotel", hotel.getId()).eq("orderuser", user1.getId());
        }else if (user.getRole()==3){
            QueryWrapper<Hotel> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("hotelname", query);
            Hotel hotel = hotelMapper.selectOne(queryWrapper2);
            queryWrapper1.eq("orderuser", user.getId()).eq("orderhotel",hotel.getId());
        }
        Page<Orders> ordersPage1 = ordersMapper.selectPage(ordersPage, queryWrapper1);
        System.out.println(ordersPage1);
        List<OrderVO> records= new ArrayList<>();
        Map result = new HashMap();
        for (Orders order :
                ordersPage1.getRecords()) {
            User user1 = userMapper.selectById(order.getOrderuser());
            Hotel hotel1 = hotelMapper.selectById(order.getOrderhotel());
            QueryWrapper<Room> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("roomcode", order.getRoomcode()).eq("hotelid", order.getOrderhotel());
            Room room = roomMapper.selectOne(queryWrapper2);
            OrderVO orderVO = new OrderVO();
            orderVO.setId(order.getId());
            orderVO.setCreatedate(order.getCreatedate());
            orderVO.setOrderuser(user1.getUsername());
            orderVO.setOrderhotel(hotel1.getHotelname());
            orderVO.setOrdercompany(order.getOrdercompany());
            orderVO.setOrdernumber(order.getOrdernumber());
            orderVO.setIndate(order.getIndate());
            orderVO.setOutdate(order.getOutdate());
            orderVO.setOrderprice(order.getOrderprice());
            orderVO.setRoomcode(order.getRoomcode());
            orderVO.setRoomtype(room.getRoomtype());
            records.add(orderVO);
        }
        Page<OrderVO> orderVOPage = new Page<OrderVO>(ordersPage1.getCurrent(), ordersPage1.getSize(), ordersPage1.getTotal());
        orderVOPage.setRecords(records);
        orderVOPage.setOrders(ordersPage1.getOrders());
        orderVOPage.setCountId(ordersPage1.getCountId());
        orderVOPage.setPages(ordersPage1.getPages());
        orderVOPage.setMaxLimit(ordersPage1.getMaxLimit());
        return orderVOPage;
    }

    //退房
    @Override
    public Boolean updateStatus(Integer roomcode,String orderhotel) {
        QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("hotelname",orderhotel);
        Hotel hotel = hotelMapper.selectOne(queryWrapper1);
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roomcode",roomcode).eq("hotelid", hotel.getId());
        Room room = roomMapper.selectOne(queryWrapper);
        if (room.getRoomstate()==3){
            room.setRoomstate(1);
        }else{
            return false;
        }
        roomMapper.updateById(room);
        return true;
    }

    @Override
    public OrderVO getNow(Integer id) {
        Orders orders = ordersMapper.selectById(id);

        User user1 = userMapper.selectById(orders.getOrderuser());
        Hotel hotel1 = hotelMapper.selectById(orders.getOrderhotel());
        OrderVO orderVO = new OrderVO();
        orderVO.setCreatedate(orders.getCreatedate());
        orderVO.setOrderuser(user1.getUsername());
        orderVO.setOrderhotel(hotel1.getHotelname());
        orderVO.setOrdercompany(orders.getOrdercompany());
        orderVO.setOrdernumber(orders.getOrdernumber());
        orderVO.setIndate(orders.getIndate());
        orderVO.setOutdate(orders.getOutdate());
        orderVO.setOrderprice(orders.getOrderprice());
        orderVO.setRoomcode(orders.getRoomcode());
        QueryWrapper<Room> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("roomcode", orders.getRoomcode()).eq("hotelid", hotel1.getId());
        Room room = roomMapper.selectOne(queryWrapper2);
        orderVO.setRoomtype(room.getRoomtype());
        return orderVO;
    }

    @Override
    public Boolean changeStatus(Integer roomcode, String orderhotel) {
        QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("hotelname",orderhotel);
        Hotel hotel = hotelMapper.selectOne(queryWrapper1);
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roomcode",roomcode).eq("hotelid", hotel.getId());
        Room room = roomMapper.selectOne(queryWrapper);
        if (room.getRoomstate()==2){
            room.setRoomstate(3);
        }else{
            return false;
        }
        roomMapper.updateById(room);
        return true;
    }

    @Override
    public Page<OrderVO> getLoginOrder(String usercode, Integer current, Integer size) {
        Page<Orders> page = new Page<>(current,size);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usercode", usercode);
        User user = userMapper.selectOne(queryWrapper);
        System.out.println("userrole"+user.getRole());
        Page<Orders> ordersPage1 = new Page<>();
        if (user.getRole()==2){
            QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("userid", user.getId());
            Hotel hotel = hotelMapper.selectOne(queryWrapper1);
            QueryWrapper<Orders> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("orderhotel", hotel.getId());
            ordersPage1= ordersMapper.selectPage(page, queryWrapper2);
        }else if(user.getRole()==3){
            QueryWrapper<Orders> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("orderuser", user.getId());
            ordersPage1 = ordersMapper.selectPage(page, queryWrapper1);
        }else{
            ordersPage1 = ordersMapper.selectPage(page,null);
        }

        List<OrderVO> records= new ArrayList<>();
        Map result = new HashMap();
        for (Orders order :
                ordersPage1.getRecords()) {
            QueryWrapper<Room> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("roomcode", order.getRoomcode()).eq("hotelid", order.getOrderhotel());
            Room room = roomMapper.selectOne(queryWrapper1);
            User user1 = userMapper.selectById(order.getOrderuser());
            Hotel hotel1 = hotelMapper.selectById(order.getOrderhotel());
            OrderVO orderVO = new OrderVO();
            orderVO.setId(order.getId());
            orderVO.setCreatedate(order.getCreatedate());
            orderVO.setOrderuser(user1.getUsername());
            orderVO.setOrderhotel(hotel1.getHotelname());
            orderVO.setOrdercompany(order.getOrdercompany());
            orderVO.setOrdernumber(order.getOrdernumber());
            orderVO.setIndate(order.getIndate());
            orderVO.setOutdate(order.getOutdate());
            orderVO.setOrderprice(order.getOrderprice());
            orderVO.setRoomcode(order.getRoomcode());
            orderVO.setRoomstate(room.getRoomstate());
            QueryWrapper<Room> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("roomcode", order.getRoomcode()).eq("hotelid", order.getOrderhotel());
            Room room1 = roomMapper.selectOne(queryWrapper2);
            orderVO.setRoomtype(room1.getRoomtype());
            records.add(orderVO);
        }
        Page<OrderVO> orderVOPage = new Page<OrderVO>(ordersPage1.getCurrent(), ordersPage1.getSize(), ordersPage1.getTotal());
        orderVOPage.setRecords(records);
        orderVOPage.setOrders(ordersPage1.getOrders());
        orderVOPage.setCountId(ordersPage1.getCountId());
        orderVOPage.setPages(ordersPage1.getPages());
        orderVOPage.setMaxLimit(ordersPage1.getMaxLimit());
        System.out.println(orderVOPage);
        return orderVOPage;
    }

    @Override
    public Page<OrderVO> examOrder(String usercode, Integer current, Integer size) {
        Page<Orders> page = new Page<>(current,size);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usercode", usercode);
        User user = userMapper.selectOne(queryWrapper);
        System.out.println("userrole"+user.getRole());
        Page<Orders> ordersPage1 = new Page<>();
        if (user.getRole()==2){
            QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("userid", user.getId());
            Hotel hotel = hotelMapper.selectOne(queryWrapper1);
            QueryWrapper<Orders> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("orderhotel", hotel.getId());
            ordersPage1= ordersMapper.selectPage(page, queryWrapper2);
        }else if(user.getRole()==3){
            QueryWrapper<Orders> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("orderuser", user.getId());
            ordersPage1 = ordersMapper.selectPage(page, queryWrapper1);
        }else{
            ordersPage1 = ordersMapper.selectPage(page,null);
        }

        List<OrderVO> records= new ArrayList<>();
        Map result = new HashMap();
        for (Orders order :
                ordersPage1.getRecords()) {
            QueryWrapper<Room> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("roomcode", order.getRoomcode()).eq("hotelid", order.getOrderhotel());
            Room room = roomMapper.selectOne(queryWrapper1);
            User user1 = userMapper.selectById(order.getOrderuser());
            Hotel hotel1 = hotelMapper.selectById(order.getOrderhotel());
            OrderVO orderVO = new OrderVO();
            orderVO.setId(order.getId());
            orderVO.setCreatedate(order.getCreatedate());
            orderVO.setOrderuser(user1.getUsername());
            orderVO.setOrderhotel(hotel1.getHotelname());
            orderVO.setOrdercompany(order.getOrdercompany());
            orderVO.setOrdernumber(order.getOrdernumber());
            orderVO.setIndate(order.getIndate());
            orderVO.setOutdate(order.getOutdate());
            orderVO.setOrderprice(order.getOrderprice());
            orderVO.setRoomcode(order.getRoomcode());
            orderVO.setRoomstate(room.getRoomstate());
            QueryWrapper<Room> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("roomcode", order.getRoomcode());
            Room room1 = roomMapper.selectOne(queryWrapper2);
            orderVO.setRoomtype(room1.getRoomtype());
            if (room.getRoomstate()==4){
                records.add(orderVO);
            }
        }
        Page<OrderVO> orderVOPage = new Page<OrderVO>(ordersPage1.getCurrent(), ordersPage1.getSize(), ordersPage1.getTotal());
        orderVOPage.setRecords(records);
        orderVOPage.setOrders(ordersPage1.getOrders());
        orderVOPage.setCountId(ordersPage1.getCountId());
        orderVOPage.setPages(ordersPage1.getPages());
        orderVOPage.setMaxLimit(ordersPage1.getMaxLimit());
        System.out.println(orderVOPage);
        return orderVOPage;
    }

    @Override
    public Boolean examNow(Integer roomcode, String orderhotel) {
        QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("hotelname",orderhotel);
        Hotel hotel = hotelMapper.selectOne(queryWrapper1);
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roomcode",roomcode).eq("hotelid", hotel.getId());
        Room room = roomMapper.selectOne(queryWrapper);
        if (room.getRoomstate()==4){
            room.setRoomstate(2);
        }else{
            return false;
        }
        roomMapper.updateById(room);
        return true;
    }

    @Override
    public Boolean noexamNow(Integer roomcode, String orderhotel) {
        QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("hotelname",orderhotel);
        Hotel hotel = hotelMapper.selectOne(queryWrapper1);
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roomcode",roomcode).eq("hotelid", hotel.getId());
        Room room = roomMapper.selectOne(queryWrapper);
        if (room.getRoomstate()==4){
            room.setRoomstate(5);
        }else{
            return false;
        }
        roomMapper.updateById(room);
        return true;
    }

    //预定酒店
    @Override
    public Boolean reseOrders(OrderVO orders) {
        Boolean flag = false;
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",orders.getOrderuser());
        User user = userMapper.selectOne(queryWrapper);
        QueryWrapper<Hotel> queryWrapper1 = new QueryWrapper<>();
        int i = Integer.parseInt(orders.getOrderhotel());
        QueryWrapper<Room> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("roomcode",orders.getRoomcode()).eq("hotelid", orders.getOrderhotel());
        Room room = roomMapper.selectOne(queryWrapper2);
        room.setRoomstate(4);
        roomMapper.updateById(room);
        Orders orders1 = new Orders();
        orders1.setOrdercompany(orders.getOrdercompany());
        orders1.setOrderhotel(i);
        orders1.setOrderuser(user.getId());
        orders1.setOrdernumber(orders.getOrdernumber());
        orders1.setOrderprice(orders.getOrderprice());
        orders1.setCreatedate(new Date());
        orders1.setIndate(orders.getIndate());
        orders1.setOutdate(orders.getOutdate());
        orders1.setRoomcode(orders.getRoomcode());
        int insert = ordersMapper.insert(orders1);
        if (insert>0){
            flag = true;
        }
        return flag;
    }

    @Override
    public Page<OrderVO> getHotelOrder(String query, Integer current, Integer size) {
        Page<Orders> page = new Page<>(current, size);
        QueryWrapper<Hotel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hotelname", query);
        Hotel hotel = hotelMapper.selectOne(queryWrapper);
        QueryWrapper<Orders> queryWrapper1 = new QueryWrapper<>();
        queryWrapper.eq("orderhotel", queryWrapper1);
        Page<Orders> ordersPage1 = ordersMapper.selectPage(page, queryWrapper1);

        List<OrderVO> records= new ArrayList<>();
        Map result = new HashMap();
        for (Orders order :
                ordersPage1.getRecords()) {
            QueryWrapper<Room> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("roomcode", order.getRoomcode()).eq("hotelid", order.getOrderhotel());
            Room room = roomMapper.selectOne(queryWrapper2);
            User user1 = userMapper.selectById(order.getOrderuser());
            Hotel hotel1 = hotelMapper.selectById(order.getOrderhotel());
            OrderVO orderVO = new OrderVO();
            orderVO.setId(order.getId());
            orderVO.setCreatedate(order.getCreatedate());
            orderVO.setOrderuser(user1.getUsername());
            orderVO.setOrderhotel(hotel1.getHotelname());
            orderVO.setOrdercompany(order.getOrdercompany());
            orderVO.setOrdernumber(order.getOrdernumber());
            orderVO.setIndate(order.getIndate());
            orderVO.setOutdate(order.getOutdate());
            orderVO.setOrderprice(order.getOrderprice());
            orderVO.setRoomcode(order.getRoomcode());
            orderVO.setRoomstate(room.getRoomstate());
            QueryWrapper<Room> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("roomcode", order.getRoomcode()).eq("hotelid", order.getOrderhotel());
            Room room1 = roomMapper.selectOne(queryWrapper3);
            orderVO.setRoomtype(room1.getRoomtype());
            if (room.getRoomstate()==4){
                records.add(orderVO);
            }
        }
        Page<OrderVO> orderVOPage = new Page<OrderVO>(ordersPage1.getCurrent(), ordersPage1.getSize(), ordersPage1.getTotal());
        orderVOPage.setRecords(records);
        orderVOPage.setOrders(ordersPage1.getOrders());
        orderVOPage.setCountId(ordersPage1.getCountId());
        orderVOPage.setPages(ordersPage1.getPages());
        orderVOPage.setMaxLimit(ordersPage1.getMaxLimit());
        System.out.println(orderVOPage);
        return orderVOPage;
    }

    @Override
    public Page<OrderVO> getOrderByHotel(String usercode, String query, Integer current, Integer size) {
        Page<Orders> page = new Page(current, size);
        //获取当前登录用户
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("usercode", usercode);
        User user = userMapper.selectOne(queryWrapper1);
        //获取当前酒店
        QueryWrapper<Hotel> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("userid", user.getId());
        Hotel hotel = hotelMapper.selectOne(queryWrapper2);
        //获取要查询的用户  的所有订单
        QueryWrapper<User> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("username", query);
        User user1 = userMapper.selectOne(queryWrapper3);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderuser",user1.getId()).eq("orderhotel", hotel.getId());
        Page<Orders> ordersPage1 = ordersMapper.selectPage(page, queryWrapper);
        List<OrderVO> records= new ArrayList<>();
        Map result = new HashMap();
        for (Orders order :
                ordersPage1.getRecords()) {
            QueryWrapper<Room> queryWrapper4= new QueryWrapper<>();
            queryWrapper4.eq("roomcode", order.getRoomcode()).eq("hotelid", order.getOrderhotel());
            Room room = roomMapper.selectOne(queryWrapper4);
            User user2 = userMapper.selectById(order.getOrderuser());
            Hotel hotel1 = hotelMapper.selectById(order.getOrderhotel());
            OrderVO orderVO = new OrderVO();
            orderVO.setId(order.getId());
            orderVO.setCreatedate(order.getCreatedate());
            orderVO.setOrderuser(user2.getUsername());
            orderVO.setOrderhotel(hotel1.getHotelname());
            orderVO.setOrdercompany(order.getOrdercompany());
            orderVO.setOrdernumber(order.getOrdernumber());
            orderVO.setIndate(order.getIndate());
            orderVO.setOutdate(order.getOutdate());
            orderVO.setOrderprice(order.getOrderprice());
            orderVO.setRoomcode(order.getRoomcode());
            orderVO.setRoomstate(room.getRoomstate());
            orderVO.setRoomtype(room.getRoomtype());
            records.add(orderVO);

        }
        Page<OrderVO> orderVOPage = new Page<OrderVO>(ordersPage1.getCurrent(), ordersPage1.getSize(), ordersPage1.getTotal());
        orderVOPage.setRecords(records);
        orderVOPage.setOrders(ordersPage1.getOrders());
        orderVOPage.setCountId(ordersPage1.getCountId());
        orderVOPage.setPages(ordersPage1.getPages());
        orderVOPage.setMaxLimit(ordersPage1.getMaxLimit());
        System.out.println(orderVOPage);
        return orderVOPage;
    }

    @Override
    public List<Object> getOrderData(String usercode) {
        QueryWrapper<User> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("usercode", usercode);
        User user = userMapper.selectOne(queryWrapper1);
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        if (user.getRole()==2){
            QueryWrapper<Hotel> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("userid", user.getId());
            Hotel hotel = hotelMapper.selectOne(queryWrapper2);
            queryWrapper.eq("orderhotel", hotel.getId());
        }else if(user.getRole()==3){
            queryWrapper.eq("orderuser", user.getId());
        }
        queryWrapper.select("createdate","count(*) as total","sum(orderprice) as price")
                .groupBy("createdate");
        List<Orders> orders = ordersMapper.selectList(queryWrapper);
        List<Object> res = new ArrayList<>();
        List<Integer> price = new ArrayList<>();
        ArrayList<String> createdates = new ArrayList<>();
        ArrayList<Integer> total = new ArrayList<>();
        for (Orders order:
             orders) {
            total.add(order.getTotal());
            price.add(order.getPrice());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            createdates.add(sdf.format(order.getCreatedate()));
        }
        res.add(total);
        res.add(price);
        res.add(createdates);
        return res;
    }

}
